<?php


namespace app\api\controller;


use app\common\model\Cate;

class Bill extends Base
{

    //分类排序
    public function cateSort()
    {
        $user = $this->currentUser();
        $sort_data = $this->essentialParam('sort_data');
        $cate_type = $this->essentialParam('cate_type');
        Cate::setSort($user, $cate_type, $sort_data);
        return $this->response('请求成功');
    }

    //报表
    public function reportTable()
    {
        $user = $this->currentUser();
        $year = $this->essentialParam('year');
        $type = $this->essentialParam('type');
        $month = $this->optionalParam('month');
        $week = date("W");
        $list = \app\common\model\Bill::getReportTable($user, $year, $month, $week, $type);
        $this->insertResponse('data', $list);
        return $this->response("请求成功");
    }

    //指定年账目记录
    public function yearLog()
    {
        $user = $this->currentUser();
        $year = $this->essentialParam('year');
        $list = \app\common\model\Bill::getYearBillLog($user, $year);
        $this->insertResponse('data', $list);
        return $this->response('请求成功');
    }

    //指定月份账目记录
    public function monthLog()
    {
        $user = $this->currentUser();
        $month = $this->essentialParam('month');
        $year = $this->essentialParam('year');
        $list = \app\common\model\Bill::getMonthBillLog($user, $year, $month);
        $this->insertResponse('data', $list);
        //添加用户信息
        $this->insertResponse('user_info', [
            "avatar"=>$user['avatar'],
            "tel"=>show_star($user['mobile'], 3, 4)
        ]);
        return $this->response('请求成功');
    }

    //批量添加预算
    public function batchAddBudget()
    {
        $user = $this->currentUser();
        $data = $this->essentialParam("data");
        $bill_data = json_decode($data, true);
        foreach ($bill_data as $item) {
            \app\common\model\Bill::setBudget($user, $item['year'], $item['month'], $item['money']);
        }
        return $this->response('添加成功');

    }

    //添加预算记录
    public function addBudget()
    {
        $user = $this->currentUser();
        $month = $this->essentialParam('month');
        $year = $this->essentialParam("year");
        $money = $this->essentialParam("money");
        \app\common\model\Bill::setBudget($user, $year, $month, $money);
        return $this->response("设置成功");
    }

    //删除账目
    public function delBill()
    {
        $user = $this->currentUser();
        $bill_id = $this->essentialParam('bill_id');
        \app\common\model\Bill::delBill($user['id'], $bill_id);
        return $this->response('删除成功');
    }


//修改账目
    public function editBill()
    {
        $user = $this->currentUser();
        $bill_id = $this->essentialParam('bill_id');
        $total = $this->essentialParam('total');
        $add_time = $this->essentialParam('add_time');
        $remarks = $this->essentialParam('remarks');
        \app\common\model\Bill::editBill($user['id'], $total, $bill_id, $add_time, $remarks);
        return $this->response('操作成功');
    }

    public function batchAddBill()
    {
        $user = $this->currentUser();
        $data = $this->essentialParam("data");
        $bill_data = json_decode($data, true);
        foreach ($bill_data as $item) {
            \app\common\model\Bill::addBill($user['id'], $item['total'], $item['cate_id'], $item['cate_type'], $item['add_time'], $item['remarks']);
        }
        return $this->response('添加成功');
    }

    //添加记账
    public function addBill()
    {
        $user = $this->currentUser();
        $total = $this->essentialParam('total');
        $cate_id = $this->essentialParam('cate_id');
        $cate_type = $this->essentialParam('cate_type');
        $remarks = $this->essentialParam('remarks');
        $add_time = $this->essentialParam('add_time');
        \app\common\model\Bill::addBill($user['id'], $total, $cate_id, $cate_type, $add_time, $remarks);
        return $this->response('添加成功');
    }

    //修改类目
    public function editCate()
    {
        $user = $this->currentUser();
        $cate_id = $this->essentialParam('cate_id');
        $cate_name = $this->essentialParam('cate_name');
        $cate_img = $this->essentialParam('cate_img');
        Cate::editCate($user['id'], $cate_id, $cate_name, $cate_img);
        return $this->response('操作成功');

    }

    //添加类目
    public function addCate()
    {
        $user = $this->currentUser();
        $cate_id = $this->essentialParam('cate_id');
        $cate_type = $this->essentialParam('cate_type');
        $cate_name = $this->essentialParam('cate_name');
        $cate_img = $this->essentialParam('cate_img');
        Cate::addCate($user['id'], $cate_type, $cate_name, $cate_img, $cate_id);
        return $this->response('操作成功');

    }

    //删除类目
    public function delCate()
    {
        $user = $this->currentUser();
        $cate_id = $this->essentialParam('cate_id');
        Cate::delCate($user['id'], $cate_id);
        return $this->response('操作成功');
    }

    //获取账目分类
    public function cate()
    {
        $user = $this->currentUser();
        $cate_type = $this->optionalParam('cate_type');
        $cate_list = Cate::getCateList($user['id'], $cate_type);
        $this->insertResponse("cate_list", $cate_list);
        return $this->response('请求成功');
    }


}